/**
 * SearchActivity.java Create on 2012-7-30     
 *      
 * Copyright (c) 2012-7-30 by yiming     
 *      
 * @author <a href="alenzhangym@hotmail.com">Yiming</a>     
 * @version 1.0 
 */

package com.yimi.image;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
 
import com.yimi.actions.SearchListener;
import com.yimi.common.MessageType;
import com.yimi.content.ImageListAdapt;
import com.yimi.content.ImageListModel;
import com.yimi.content.ImageListModelImpl;
import com.yimi.search.AHApplication;
import com.yimi.search.SearchImageService;
import com.yimi.service.BaseImageItem;
import com.yimi.service.Entity;
import com.yimi.service.ImageList;
import com.yimi.service.SearchService;
import com.yimi.ui.PullToRefreshListView;
import com.yimi.ui.PullToRefreshListView.OnRefreshListener;
import com.yimi.util.ImageSearchUtil;

/**
 * TODO 显示搜索图片的activity
 * 
 * @author Yiming
 * @version 1.0
 */
public class SearchActivity extends Activity implements SearchListener, OnClickListener {
    AHApplication app;
    private EditText searchText;
    private Button searchButton;
    private SearchImageService searchService;
    private int page = 0;
    private PullToRefreshListView listView;
    private ImageListAdapt adapt;
    private ImageListModel model;
    private String searchName;  
    RelativeLayout adcontainer;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(this.getClass().getName(), "onCreate");
        app = (AHApplication) this.getApplication();
        this.setContentView(R.layout.searchlist);
        adcontainer = (RelativeLayout) this.findViewById(R.id.adcontainer);
        searchText = (EditText) this.findViewById(R.id.account_input);
        searchButton = (Button) this.findViewById(R.id.search_button);
        searchButton.setOnClickListener(this);
        searchButton.setFocusable(true);
        listView = (PullToRefreshListView) this.findViewById(R.id.image_list);
        listView.setFocusable(true);
        searchName = searchText.getText().toString().trim();
        listView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                try {
                    if( searchText.getText()==null){
                        return;
                    }
                    if( searchText.getText().toString().trim()==""){
                        return;
                    }
                    if(!ImageSearchUtil.isRomAvailable()){
                        ImageSearchUtil.clearPreviewmage();
                    }
                    InputMethodManager inputMethodManager = (InputMethodManager)
                            getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMethodManager.hideSoftInputFromWindow(SearchActivity.this
                            .getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    listView.setLastUpdated("开始查询...");
                    String trySearchName = searchText.getText().toString().trim();
                    app.getSearchImageService().queryImages(page + 1, trySearchName);
                } catch (RemoteException e) { 
                }
            }
        });
        model = new ImageListModelImpl();
        adapt = new ImageListAdapt(this, model);
        listView.setAdapter(adapt);
        searchService = app.getSearchImageService();
        searchService.registerSearchListener(this);
    }

    /*
     * (non-Javadoc)
     * @see com.yimi.actions.SearchEvent#onEvent(int, int, java.lang.Object)
     */
    @Override
    public void onEvent(int event, int type, Entity object) {
        Log.e(this.getClass().getName(), "Onevnt");
        // dialog = ImageSearchUtil.dialogShow(dialog, SearchActivity.this,
        // "正在查询，请稍后...");
        // dialog.cancel();
        if (event == MessageType.EVENT_QUERYIMAGELISTSUCCESS) {
            Log.e(this.getClass().getName(), "搜索成功");
            ImageList list = (ImageList) object;
            if (searchName.equals(list.getSearchName())) {
                Log.e(this.getClass().getName(), "开始添加");
                Log.e(this.getClass().getName(), "添加宛城");
                if (type > page)
                {
                    model.addImageList(list);
                    this.page = type;
                }
            } else {
                Log.e(this.getClass().getName(), "创建新模型");
                model = new ImageListModelImpl();
                model.addImageList(list);
                adapt = new ImageListAdapt(this, model);
                listView.setAdapter(adapt);
                page = 1;
            }
            searchName = list.getSearchName();
            app.getSearchImageService().queryPreview(page, list);
            adapt.notifyDataSetChanged();
            listView.onRefreshComplete("搜索成功");
            return;
        } else if (event == MessageType.EVENT_QUERYIMAGELISTFAILED) {
            Toast t = Toast.makeText(this, "搜索失败", 3000);
            t.show();
            this.listView.onRefreshComplete("搜索失败");
            return;
        } else if (event == MessageType.EVENT_QUERYPRIVIEWSUCCESS) {
            BaseImageItem previewItem = (BaseImageItem) object;
            adapt.updateItem(previewItem);
            adapt.notifyDataSetChanged();
            return;
        }
    }

    public void onResume() {
        super.onResume();
        searchText.clearFocus();
        Log.e(this.getClass().getName(), "onRescume");
    }

    /*
     * (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View view) {
        Log.e(this.getClass().getName(), "click");
        if (view == searchButton) {
            try {
                if( searchText.getText()==null){
                    return;
                }
                if( searchText.getText().toString().trim()==""){
                    return;
                }
                if(!ImageSearchUtil.isRomAvailable()){
                    ImageSearchUtil.clearPreviewmage();
                }
                InputMethodManager inputMethodManager = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
                        .getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
                Log.e(this.getClass().getName(), "searchButton is click" + searchName);
                listView.setLastUpdated("开始查询..."); 
                String trySearchName = searchText.getText().toString().trim();
                searchService.queryImages(page + 1, trySearchName);
            } catch (RemoteException e) {

            }
        }
    }

    public void onDestroy() {
        super.onDestroy();
        searchService.unRegisterSearchListener(this);
    }

    public void onBackPressed() {
        stopService(new Intent(SearchActivity.this, SearchService.class));
        finish();
        System.exit(0);
        return;
    }
}
